package com.android.superobstacleavoider;

import java.io.IOException;

import com.android.superobstacleavoider.framework.Audio;
import com.android.superobstacleavoider.framework.Music;
import com.android.superobstacleavoider.framework.Sound;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;

public class GameAudio implements Audio {
	AssetManager assets;
	SoundPool soundPool;
	AudioManager m_audioManager;

	public GameAudio(/* Activity activity */Context a_context) {

		// activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
		// this.assets = activity.getAssets();
		// m_audioManager = (AudioManager)
		// a_context.getSystemService(Context.AUDIO_SERVICE);
		this.assets = a_context.getAssets();
		this.soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
	}

	@Override
	public Music newMusic(String filename) {
		try {
			AssetFileDescriptor ad = assets.openFd(filename);
			return new GameMusic(ad);
		} catch (IOException e) {
			throw new RuntimeException("Could not load music '" + filename
					+ "'");
		}
	}

	@Override
	public Sound newSound(String filename) {
		try {
			AssetFileDescriptor ad = assets.openFd(filename);
			int soundId = soundPool.load(ad, 0);
			return new GameSound(soundPool, soundId);
		} catch (IOException e) {
			throw new RuntimeException("Could not load sound '" + filename
					+ "'");
		}
	}

}
